package com.winit.project.core.branch;

import com.alibaba.fastjson.JSONObject;
import com.winit.project.util.ProjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangjian on 16/9/6.
 */
public class ProjectConfig {

    private static final Logger logger = LoggerFactory.getLogger(ProjectConfig.class);
    private static String INFO_FILE_NAME = "info.json";

    private String path;
    private String projectName;
    private String vcsPath;
    private String projectDesc;
    //工程目录下配置文件根路径 例 src/main/resources
    private String configPath;
    //关联jar工程名列表 {"isDeploy":"0","jarName":"beetle-jar"}
    private List<JSONObject> dependency;
    private boolean useSonar = false;

    public ProjectConfig(String projectName,boolean loadInfo) throws Exception{
        if(StringUtils.isEmpty(projectName)){
            logger.error("工程名不能为空");
            throw new Exception("工程名不能为空");
        }
        this.projectName = projectName;
        this.path = ProjectUtil.getProjectFilePath(projectName);
        if(loadInfo){
            this.loadFromFile();
        }
    }

    public boolean loadFromFile() throws Exception{
        JSONObject object = ProjectUtil.loadFromFile(this.path+INFO_FILE_NAME);
        this.vcsPath = object.getString("vcsPath");
        this.configPath = object.getString("configPath");
        if(!StringUtils.isEmpty(this.configPath)){
            this.configPath = this.configPath.replaceAll("\\\\","/");
        }
        this.projectDesc = object.getString("projectDesc");
        this.dependency = (List<JSONObject>) object.get("dependency");
        this.useSonar = Boolean.parseBoolean(object.getString("useSonar"));
        return true;
    }

    public boolean saveToFile() throws Exception{
        JSONObject object = new JSONObject();
        object.put("vcsPath",this.vcsPath);
        object.put("dependency",this.dependency);
        if(!StringUtils.isEmpty(this.configPath)){
            object.put("configPath",this.configPath.replaceAll("\\\\","/"));
        }
        object.put("useSonar",this.useSonar);
        object.put("projectDesc",this.projectDesc);
        return ProjectUtil.saveToFile(this.path+INFO_FILE_NAME,object);
    }

    public String getPath(){
        return this.path;
    }

    public String getVcsPath() {
        return vcsPath;
    }

    public void setVcsPath(String vcsPath) {
        this.vcsPath = vcsPath;
    }

    public String getProjectName() {
        return projectName;
    }

    public String getConfigPath() {
        if(StringUtils.isEmpty(this.configPath)){
            return "src/main/resources";
        }
        return configPath;
    }

    public void setConfigPath(String configPath) {
        this.configPath = configPath;
    }

    public List<JSONObject> getDependency() {
        if(dependency == null){
            return new ArrayList();
        }
        return dependency;
    }

    public void setDependency(List<JSONObject> dependency) {
        this.dependency = dependency;
    }

    public boolean isUseSonar() {
        return useSonar;
    }

    public void setUseSonar(boolean useSonar) {
        this.useSonar = useSonar;
    }

    public String getProjectDesc() {
        return projectDesc;
    }

    public void setProjectDesc(String projectDesc) {
        this.projectDesc = projectDesc;
    }
}
